//  责任链模式是为了避免发送请求着与请求处理着耦合在一起，将请求处理着
//  通过一个链条连接在一起。

#include <iostream>
#include <string>
#include <vector>
#include <memory>

using namespace std;

class Handler
{
    private:
    Handler* m_next;
    
    public:
    virtual void setNext(Handler* next){
        m_next = next;
    }
    virtual Handler* getNext(){
        return m_next;
    }
    virtual void handleRequest(string request) = 0;
    virtual ~Handler(){};
};

class HandlerA : public Handler
{
    public:
    void handleRequest(string request){
        if(request == "1"){
            std::cout<<"Finised in me: Mr. 1."<<std::endl;
        }else
        {
            if(getNext() != nullptr){
                getNext()->handleRequest(request);
            }else{
                std::cout<<"No body deal with it."<<std::endl;
            }
        }
        
    }
};

class HandlerB : public Handler
{
    public:
    void handleRequest(string request){
        if(request == "2"){
            std::cout<<"Finised in me: Mr. 2."<<std::endl;
        }else
        {
            if(getNext() != nullptr){
                getNext()->handleRequest(request);
            }else{
                std::cout<<"No body deal with it."<<std::endl;
            }
        }
        
    }
};


class Responsibility
{
    private:
    Handler* m_handlerA;
    Handler* m_handlerB;
    
    public:
    Responsibility(){
        m_handlerA = new HandlerA();
        m_handlerB = new HandlerB();
        m_handlerA->setNext(m_handlerB);
    }

    void handleRequest(string request){
        m_handlerA->handleRequest(request);
    }

    ~Responsibility(){
        if(m_handlerA){
            delete m_handlerA;
        }
        if(m_handlerB){
            delete m_handlerB;
        }
    }


};


int main(int argc, char const *argv[])
{
    unique_ptr<Responsibility> response(new Responsibility());
    response->handleRequest("1");
    response->handleRequest("2");
    response->handleRequest("3");
    
    return 0;
}
